package cn.zcx;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * description:定时关机
 * author:zcx
 * lenovo
 * 时间：2022.02.07.19.30
 */
public class T10 {
    public static void main(String[] args) {
        //多长时间后关机5s
        new Thread(()->{
            long t1=System.currentTimeMillis();
            while (true){
                long ok=System.currentTimeMillis()-t1;
                if(ok>=5*1000)
                    break;
            }
            System.out.println("系统正在关机5....");
        }).start();
        //
        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("系统正在关机10s");
        }).start();
        //指定时间关机2021-2-7 21：30：00
        new Thread(()->{
            String s="2022-2-7 21:30:59";
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            long end=0;
            try {
                end=sdf.parse(s).getTime();
            } catch (ParseException e) {
                e.printStackTrace();
            }
            try {
                TimeUnit.MILLISECONDS.sleep(end-System.currentTimeMillis());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.printf("%s - 系统正在关机%n",s);
        }).start();
    }
}
